package cn.bgs.servicewebsocket.controller;

import cn.bgs.serviceutils.utils.CSException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/10/19-8:39
 */
@Component
@ServerEndpoint(value = "/webSocket/{userName}")
@Slf4j
public class WebSocket {
    private Session session;
    private static ConcurrentHashMap<String, WebSocket> webSockets= new ConcurrentHashMap<>();

    private HttpSession httpSession;
    private String userName="";
    @OnOpen
    public void onOpen(Session session, EndpointConfig config, @PathParam("userName") String userName){
        //创建session连接
//        HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        this.session=session;
//        this.httpSession=httpSession;
        //获取session用户
//        String name = (String) httpSession.getAttribute("user");
        this.userName=userName;
//        System.out.println(name);
        //当前用户存到容器中
        webSockets.put(userName,this);
        Set<String> names = getNames();
        try {
            for (String name:names){
                webSockets.get(name).sendMessage(userName+"加入了聊天");
            }
        } catch (IOException e) {
            throw new CSException(20001,"系统消息发送失败");
        }
        log.info("连接数：{},连接人：{}",webSockets.size(),userName);
    }

    public Set<String> getNames(){
     return  webSockets.keySet();
    }
    @OnClose
    public void OnClose(){
        webSockets.remove(userName,this);
        Set<String> names = getNames();
            try {
                for (String name:names){
                    webSockets.get(name).sendMessage(userName+"退出了聊天");
                }
            } catch (IOException e) {
                throw new CSException(20001,"系统消息发送失败");
            }
        log.info("{}断开连接：{}",userName,webSockets.size());
    }

    @OnMessage
    public void OnMessage(String message){
        log.info("收到消息：{},来自：{}", message,userName);
        Set<String> names = getNames();
                try {
                    for (String name:names){
                        webSockets.get(name).sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new CSException(20001,"消息发送失败");
                }
    }

    public void sendMessage(String message) throws IOException {
        try {
            this.session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            throw new CSException(20001,"消息发送失败");
        }
    }

    public void sendMessageInfo(String toName,String message) throws IOException {
        try {
            webSockets.get(toName).sendMessage(message);
        } catch (IOException e) {
            throw new CSException(20001,"消息发送失败");
        }
    }




}
